package DataStructures.Project3.Group3.s12cs342;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom.DataConversionException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import android.R;
import android.content.Context;
import android.content.res.Resources;

public class XMLParser {

	Context currentContext;  //Current applications context
	ArrayList<QuizContainer> quizzes; //Holds Quizzes
	
	/**
	 * Construct a parser to read an XML file.
	 * @param c - Current Activities context.
	 */
	public XMLParser(Context c){
		currentContext = c;
		quizzes = new ArrayList<QuizContainer>();
	}
	
	/**
	 * Parses an XML file to create a quiz.
	 */
	public String parseXML(){
		SAXBuilder builder = new SAXBuilder();
		Document document = null;
	
      
		
		
		
		try {
			document = builder.build(currentContext.openFileInput("myfile"));	//File to parse
			
		} catch (JDOMException e) {
			
			return e + " ";
		} catch (IOException e) {
			
			return e +" ";
		}  
		
		Element root = document.getRootElement();	//Gets the head of the file.
		
		List rowCollection = root.getChildren("Quiz");	//List of Collections children 
		for(int i = 0; i < rowCollection.size(); i++){
			
			
			
			Element quiz = (Element) rowCollection.get(i);
			String name = quiz.getAttribute("name").getValue();
			String description = quiz.getAttribute("description").getValue();
			String subject = quiz.getAttribute("subject").getValue();
			
			QuizContainer temp = new QuizContainer(name, description, subject);
			List rowQuizes = quiz.getChildren("Question");	//List of Quizzes children 
			
		for (int j = 0; j < rowQuizes.size(); j++) {
			Element questions = (Element) rowQuizes.get(j);
			QuestionContainer quescontainer;
			int type = 0;
			try {
				type = questions.getAttribute("type").getIntValue();
			} catch (DataConversionException e) {
				e.printStackTrace();
			}
			
			String question = questions.getAttribute("question").getValue();
			
			ArrayList<String> answers = new ArrayList<String>();
			answers.add(questions.getAttribute("a").getValue());
			answers.add(questions.getAttribute("b").getValue());
			answers.add(questions.getAttribute("c").getValue());
			answers.add(questions.getAttribute("d").getValue());
			
			String correctAnswer = questions.getAttribute("answer").getValue();
			
			int points = 0 ;
			try {
				points = questions.getAttribute("points").getIntValue();
			} catch (DataConversionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			quescontainer = new QuestionContainer(type, question, answers, correctAnswer, points);
			
			temp.addQuestion(quescontainer);
			
		}
		quizzes.add(temp);
		
		}
		return "s";


	}
	
	/**
	 * Returns a quiz containing questions.
	 * @return - quiz
	 */
	public ArrayList<QuizContainer> getQuizzes(){
		return quizzes;
	}

}
